package controller

import (
	"gitee.com/molonglove/goboot/gin"
	"helper-api/app/constant"
	"helper-api/app/models/request"
	"helper-api/app/models/response"
	"helper-api/app/service"
	"helper-api/utils"
	"net/http"
	"strconv"
)

var User = new(UserController)

type UserController struct{}

// Login 用户登录
func (u *UserController) Login(c *gin.Context) {
	var (
		err       error
		customErr *response.BusinessError
		param     request.UserLoginRequest
		result    response.UserLoginResponse
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail("请求参数错误"))
		return
	}
	if !utils.IsEnglishChineseNumericOnly(param.Username) {
		c.JSON(http.StatusOK, response.Fail("请求参数错误"))
		return
	}
	// 登录
	if result, customErr = service.User.Login(&param); err != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// Logout 登出
func (u *UserController) Logout(c *gin.Context) {
	var (
		userId int64
		err    *response.BusinessError
	)
	userId = c.GetInt64(constant.HeaderUserKey)
	if err = service.User.Logout(userId); err != nil {
		c.JSON(http.StatusOK, response.ResultCustom(err))
		return
	}
	c.JSON(http.StatusOK, response.Ok(response.Menus[response.LogoutSuccess]))
}

// Register 注册
func (u *UserController) Register(c *gin.Context) {
	var (
		err       error
		customErr *response.BusinessError
		param     request.UserRegisterRequest
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail("请求参数错误"))
		return
	}
	if !utils.IsEnglishChineseNumericOnly(param.Username) {
		c.JSON(http.StatusOK, response.Fail("账户名称仅支持字母、数字和中文"))
		return
	}
	if customErr = service.User.Register(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok("注册成功，去登录吧！"))
}

// SendEmailCode 发送邮箱验证码
func (u *UserController) SendEmailCode(c *gin.Context) {
	var (
		email     string
		customErr *response.BusinessError
	)
	email = c.Query("email")
	if customErr = service.User.SendEmail(email); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok("发送成功"))
}

func (u *UserController) GetUserInfo(c *gin.Context) {
	c.JSON(http.StatusOK, response.Ok(response.NewUserInfo()))
}

// UserPage 用户分页查询
func (u *UserController) UserPage(c *gin.Context) {
	var (
		page      *response.PageData
		customErr *response.BusinessError
		param     request.UserPageRequest
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	if page, customErr = service.User.UserPage(param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(page))
}

// UserDetail 获取用户
func (u *UserController) UserDetail(c *gin.Context) {
	var (
		result    *response.UserDetailResponse
		customErr *response.BusinessError
		id        int64
		idStr     string
		err       error
	)
	if idStr = c.Query("id"); idStr == "" {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	if id, err = strconv.ParseInt(idStr, 10, 64); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	if result, customErr = service.User.UserDetail(id); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// UserUpdate 用户编辑
func (u *UserController) UserUpdate(c *gin.Context) {
	var (
		param     request.UserUpdateRequest
		customErr *response.BusinessError
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail("请求参数错误"))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if customErr = service.User.Update(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok("更新成功"))
}

// ResetPassword 重置密码
func (u *UserController) ResetPassword(c *gin.Context) {
	var (
		customErr *response.BusinessError
		id        int64
		idStr     string
		err       error
	)
	if idStr = c.Query("id"); idStr == "" {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	if id, err = strconv.ParseInt(idStr, 10, 64); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	if customErr = service.User.ResetPassword(id, c.GetInt64(constant.HeaderUserKey)); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok("重置密码成功"))
}

// UserDelete 用户删除
func (u *UserController) UserDelete(c *gin.Context) {
	var (
		userId    int64
		param     string
		ids       []int64
		customErr *response.BusinessError
	)
	if param = c.DefaultQuery("ids", ""); param == "" {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	if ids = utils.StrToArray(param); len(ids) <= 0 {
		c.JSON(http.StatusOK, response.Ok("暂无数据需要操作"))
		return
	}
	userId = c.GetInt64(constant.HeaderUserKey)
	if customErr = service.User.DeleteUser(ids, userId); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(response.Menus[response.DeleteSuccess]))
}
